//
//  KBEmoticonsCell.swift
//  05-表情键盘
//
//  Created by kimbo on 15/12/28.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

class KBEmoticonsCell: UICollectionViewCell {
    
    // MARK: - 属性
    var emoticon: KBEmoticon? {
        didSet {
            //1 设置其他表情
            if let png = emoticon?.pngString {
                // 设置cell图片
                emoticonBtn.setImage(UIImage(contentsOfFile: png), forState: UIControlState.Normal)
            } else {
                // 没有图片就设置为空(重用)
                emoticonBtn.setImage(nil, forState: UIControlState.Normal)
            }
            
            //2 设置emoji表情
            emoticonBtn.setTitle(emoticon?.emoji, forState: UIControlState.Normal)
            
            //3 设置删除按钮(空白不用设置就是空白)
            if emoticon!.removeEmoticon {
                emoticonBtn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Normal)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI() //设置UI
    }

    func setupUI() {
        contentView.addSubview(emoticonBtn)
        
        // 设置frame(注意不要用frame，否则除了第一个，其他的会偏移)
        emoticonBtn.frame = CGRectInset(bounds, 3, 3)
        
    }
    
    // MARK: - 懒加载
    /* 按钮，用于显示图片和emoji表情 */
    private lazy var emoticonBtn: UIButton = {
        let emoticonBtn = UIButton()
        
        emoticonBtn.titleLabel?.font = UIFont.systemFontOfSize(32)
        
        emoticonBtn.userInteractionEnabled = false
        
        return emoticonBtn
    }()
}


